package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.entity.EcologyReforgeConf;
import com.sc.nft.enums.*;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.EcologyHatchCollecionShareProfitConf;
import com.sc.nft.mapper.EcologyHatchCollecionShareProfitConfMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 生态藏品分润配置(sc_ecology_hatch_collecion_share_profit_conf)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-12-06 18:46:15
 */
@Slf4j
@Repository
public class EcologyHatchCollecionShareProfitConfDao extends ServiceImpl<EcologyHatchCollecionShareProfitConfMapper, EcologyHatchCollecionShareProfitConf> {

    public Integer getCountByConfIdAndCollectionId(Long confId, Long collectionId) {
        return baseMapper.getCountByConfIdAndCollectionId(confId, collectionId);
    }

    public List<EcologyHatchCollecionShareProfitConf> getListByConfId(Long id) {
        LambdaQueryWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getConfId, id);
        return list(wrapper);
    }

    public EcologyHatchCollecionShareProfitConf getOneByConfIdAndType(Long confId, EcologyHatchShareProfitTypeEnum type) {
        LambdaQueryWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getConfId, confId);
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getType, type);
        wrapper.orderByDesc(EcologyHatchCollecionShareProfitConf::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public boolean updateDistributionScaleById(Long id, BigDecimal distributionScale) {
        LambdaUpdateWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getId, id);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getDistributionScale, distributionScale);
        return update(wrapper);
    }

    public boolean updateScaleById(Long id, Integer serviceCenterNewUserNum, Integer daoNewUserNum, BigDecimal resonanceDaoDivideIntoScale, BigDecimal resonanceServiceCenterDaoDivideIntoScale) {
        LambdaUpdateWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getId, id);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getServiceCenterNewUserNum, serviceCenterNewUserNum);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getDaoNewUserNum, daoNewUserNum);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getResonanceDaoDivideIntoScale, resonanceDaoDivideIntoScale);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getResonanceServiceCenterDaoDivideIntoScale, resonanceServiceCenterDaoDivideIntoScale);
        return update(wrapper);
    }

    public Boolean getCountByConfId(Long id) {
        LambdaQueryWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getConfId, id);
        return count(wrapper) > 0;
    }

    public EcologyHatchCollecionShareProfitConf getOneByCollectionIdAndType(Long collectionId, EcologyHatchShareProfitTypeEnum type) {
        LambdaQueryWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getCollectionId, collectionId);
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getType, type);
        wrapper.orderByDesc(EcologyHatchCollecionShareProfitConf::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    /**
     * 查询每个藏品的配置
     *
     * @param type
     * @return
     */
    public List<EcologyHatchCollecionShareProfitConf> getListGroupCollectionIdByType(EcologyHatchShareProfitTypeEnum type) {
        LambdaQueryWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getType, type);
        wrapper.eq(
                EcologyHatchCollecionShareProfitConf::getSeparateAccountType,
                EcologyHatchShareProfitSeparateAccountTypeEnum.DYNAMIC_SCALE);
        wrapper.groupBy(EcologyHatchCollecionShareProfitConf::getCollectionId);
        return list(wrapper);
    }

    public boolean updateDivideScaleByCollectionIdAndType(Long collectionId, BigDecimal platformDivideScale, BigDecimal daoDivideIntoScale) {
        LambdaUpdateWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getCollectionId, collectionId);
        wrapper.in(EcologyHatchCollecionShareProfitConf::getType, EcologyHatchShareProfitTypeEnum.MARKET_VALUE_MANAGEMENT, EcologyHatchShareProfitTypeEnum.BOTH_PARTIES_ACCOUNT);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getPlatformDivideScale, platformDivideScale);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getDaoDivideIntoScale, daoDivideIntoScale);
        return update(new EcologyHatchCollecionShareProfitConf(), wrapper);
    }

    public boolean updateDaoId(Long id, Long daoId) {
        LambdaUpdateWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getId, id);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getDaoId, daoId);
        return update(wrapper);
    }

    public boolean updateServiceCenterDaoId(Long id, Long daoId) {
        LambdaUpdateWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getId, id);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getServiceCenterDaoId, daoId);
        return update(wrapper);
    }

    public boolean updateSeparateAccountTypeByConfId(Long confId, EcologyHatchShareProfitSeparateAccountTypeEnum separateAccountType) {
        LambdaUpdateWrapper<EcologyHatchCollecionShareProfitConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EcologyHatchCollecionShareProfitConf::getConfId, confId);
        wrapper.set(EcologyHatchCollecionShareProfitConf::getSeparateAccountType, separateAccountType);
        return update(wrapper);
    }

    /**
     * 删除生态分润配置
     *
     * @param confId
     * @return
     */
    public boolean delByConfId(Long confId) {
        return this.lambdaUpdate()
                .eq(EcologyHatchCollecionShareProfitConf::getConfId, confId)
                .remove();
    }
}